import request from 'src/utils/request'

// 小程序用户登录
export function login(
  code: string,
  data: object
): Promise<{ token: string; user: any }> {
  return request('wechat/authorize/program/login?code=' + code, {
    method: 'POST',
    data,
  })
}

// 获取用户信息
export function getUserInfo(): Promise<object> {
  return request('user')
}

// 修改用户信息
export function userEdit(data) {
  return request('user/edit', {
    method: 'POST',
    data,
  })
}

// 主动绑定推广人
export function spread(pid) {
  return request('user/bindSpread?spreadPid=' + pid)
}

// 修改用户手机
export function userBindPhone(data) {
  return request('bindPhone', {
    method: 'POST',
    data,
  })
}

// 地址列表
export function getAddressList() {
  return request('address/list', {
    data: { page: 1, limit: 999 },
  })
}

// 获取默认地址
export function getAddressDefault(): Promise<{
  realName: string
  phone: string
  province: string
  city: string
  cityId: number
  district: string
  detail: string
  isDefault: boolean
}> {
  return request('address/default')
}

// 获取单个地址
export function getAddressDetail(id): Promise<{
  realName: string
  phone: string
  province: string
  city: string
  cityId: number
  district: string
  detail: string
  isDefault: boolean
}> {
  return request('address/detail/' + id)
}

// 设置默认地址
export function setAddressDefault(id) {
  return request('address/default/set', {
    method: 'POST',
    data: { id },
  })
}

// 修改/添加地址
export function editAddress(data) {
  return request('address/edit', {
    method: 'POST',
    data,
  })
}

// 删除地址
export function delAddress(id) {
  return request('address/del', {
    method: 'POST',
    data: { id },
  })
}

// 获取城市列表
export function getCity() {
  return request('city/list')
}

// 门店列表
export function getStoreList() {
  return request('store/shopList')
}

// 获取产品列表
export function getProductList(): Promise<{ list: any[] }> {
  return request('products')
}

// 获取推荐产品
export function getProductLevel() {
  return request('product/level')
}

// 获取产品详情
export function getProductDetail(id) {
  return request('product/detail/' + id)
}

// 加入购物车
export function addCart(data) {
  return request('cart/save', {
    method: 'POST',
    data,
  })
}

// 订单确认获取订单详细信息
export function orderConfirm(data) {
  return request('order/confirm', {
    method: 'POST',
    data: {
      isNew: true,
      addAgain: false,
      secKill: false,
      combination: false,
      bargain: false,
      ...data,
    },
  })
}

// 订单统计数据
export function getOrderData() {
  return request('order/data')
}

// 订单列表
export function getOrderList(data): Promise<any[]> {
  return request('order/list', { data })
}

// 订单详情
export function getOrder(
  orderId
): Promise<{ cartInfo: any[]; [key: string]: any }> {
  return request('order/detail/' + orderId)
}

// 订单创建
export function orderCreate(key, data) {
  return request('order/create/' + key, {
    method: 'POST',
    data,
  })
}

// 订单取消
export function orderCancel(id) {
  return request('order/cancel?id=' + id, {
    method: 'POST',
    data: { id },
  })
}

// 订单收货
export function orderTake(id) {
  return request('order/take?id=' + id, {
    method: 'POST',
    data: { id },
  })
}

// 将字符串 转base64
export function qrcodeApi(data) {
  return request('qrcode/str2base64', {
    method: 'POST',
    data,
    header: {
      'content-type': 'application/x-www-form-urlencoded',
    },
  })
}
